<?php
class dbDataEntry
        {
	/*reference to db class*/
	public $db;
	/*This associative one dimensional array should contain indices of 'name','type','code','comment' with their values*/
	public $aamaster;
	/*This two dimensional array should contain first index as numeric which is relation id and other as 'comment'*/
	public $aamaster_relation;

	/*This variable should contain the journal type  id to be inserted*/
	public $journal_type;
	/*This associative two dimensional array should contain first index as numeric and other as 'relation id'&'value' for the journal entry*/
	public $aajournal_relation;
	/*This associative two dimensional array should contain  first index as numeric and other as 'detail'&'value' */
	public $aajournal_detail;

	/*variables to return last entered master primary id*/
	public $master_id;
	public $journal_id;

/*
Function creates an master entry in the aa_master table.Pre requistee variables for this function
are $aamaster,$aamaster_relation
$aamaster should contain the master entries
$aamaster_relationship should contain the relations of the master entries and values if any
*/
	public function createMasterEntry()
		{
		try
			{
			if($this->master_id)
				{
				$this->db->beginTransaction();
				$this->updateMaster();
				$this->updateMasterRelation();
				$this->db->endTransaction();
				}
			else
				{
				$this->db->beginTransaction();
				$this->insertMaster();
				$this->insertMasterRelation();
				$this->master_id=$this->getCurrentValue("aamaster_seq");
				$this->db->endTransaction();
				}
			}
		catch(Exception $e)
			{
			echo $this->db->errors;
			$this->db->query="ROLLBACK TRANSACTION;";
			$this->db->runQuery();
 			}
		unset($this->aamaster,$this->aamaster_relation);
		}
/*

*/
	private function insertMaster()
		{
		if($this->aamaster)
			{
			$query="INSERT INTO aamaster(amcode,amname,amtype,amcomment) VALUES (";
			$query.=($this->aamaster["amcode"])?"'".$this->aamaster["amcode"]."'":"NULL";
			$query.=",'{".$this->aamaster["amname"]."}',";
			$query.=$this->aamaster["amtype"].",";
			$query.=($this->aamaster["amcomment"])?"'".$this->aamaster["amcomment"]."'":"NULL";
			$query.=")";
			$this->db->query=$query;
			$this->db->runQuery();
			return true;
			}
		else
			{
			return false;
			}
		}
/*

*/
	private function insertMasterRelation()
		{
		if($this->aamaster_relation)
			{
			foreach($this->aamaster_relation as $value)
				{
				$this->db->query="INSERT INTO aamrelation(amrmaster,amrrelation,amrpointer,amrcomment) VALUES (currval('aamaster_seq'),".$value["amrrelation"].",".$value["amrpointer"].",'".$value["amrcomment"]."');";
				$this->db->runQuery();
				}
			return true;
			}
		else
			{
			return false;
			}
		}
/*

*/
	private function updateMaster()
		{
		if($this->aamaster)
			{
			$this->db->query="UPDATE aamaster SET amcode='".$this->aamaster["amcode"]."',amname='{".trim($this->aamaster["amname"])."}',amtype=".$this->aamaster["amtype"].",amcomment='".$this->aamaster["amcomment"]."' WHERE amid=".$this->master_id;
			$this->db->runQuery();
			return true;
			}
		else
			{
			return false;
			}
		}
/*

*/
	private function updateMasterRelation()
		{
		if($this->aamaster_relation)
			{
			foreach($this->aamaster_relation as $value)
				{
				if($value["amrrelation"])
					{
					$this->db->query="UPDATE  aamrelation SET amrrelation=".$value["amrrelation"].",amrpointer=".$value["amrpointer"].",amrcomment='".$value["amrcomment"]."' WHERE  amrmaster=".$this->master_id;
					$this->db->runQuery();
					}
				}
			return true;
			}
		else
			{
			return false;
			}
		}
/*

*/
	public function createJournalEntry()
		{
		try
			{
			$this->db->beginTransaction();
			$this->insertJournal();
			$this->insertJournalRelation();
			$this->insertJournalDetail();
			$this->journal_id=$this->getCurrentValue("aajournal_seq");
			$this->db->endTransaction();
			}
		catch(Exception $e)
			{
			echo $this->db->errors;
			$this->db->query="ROLLBACK TRANSACTION;";
			$this->db->runQuery();
 			}
		unset($this->journal_type,$this->aajournal_relation,$this->aajournal_detail);
		}
/*

*/
	private function insertJournal()
		{
		if($this->journal_type)
			{
			$this->db->query="INSERT INTO aajournal(ajtype) VALUES (".$this->journal_type.")";
			$this->db->runQuery();
			return true;
			}
		else
			{
			return false;
			}
		}
/*

*/
	private function insertJournalRelation()
		{
		if($this->aajournal_relation)
			{
			foreach($this->aajournal_relation as $value)
				{
				$this->db->query="INSERT INTO aajrelation(ajrjournal,ajrrelation,ajrvalue) VALUES (currval('aajournal_seq'),".$value["relation"].",'".$value["value"]."');";
				$this->db->runQuery();
				}
			return true;
			}
		else
			{
			return false;
			}
		}
/*

*/
	private function insertJournalDetail()
		{
		if($this->aajournal_detail)
				{
				foreach($this->aajournal_detail as $value)
					{
					$this->db->query="INSERT INTO aajdetail(ajdjournal,ajddetail,ajdvalue) VALUES (currval('aajournal_seq'),'".$value["detail"]."','".$value["value"]."');";
					$this->db->runQuery();
					}
				return true;
				}
			else
				{
				return false;
				}
		}

/*
*/
	function getCurrentValue($sequence_name)
		{
		$this->db->query="select currval('".$sequence_name."') as primary_id";
		$this->db->runQuery();
		$primary_id_array=$this->db->returnArrays();
		return $primary_id_array[0]["primary_id"];
		}
        }
?>
